package com.isoft.service.user.impl;

import com.isoft.dao.user.UserDao;
import com.isoft.entity.user.User;
import com.isoft.entity.vo.LoginParam;
import com.isoft.service.user.UserService;
import com.isoft.service.email.RedisConstant;
import com.isoft.utils.BCryptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author LiuGaoMing
 * @version 1.0
 * @description:用户信息服务层实现类
 * @date 2024/6/3 11:51
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public User Login(String username, String userpass) {
        if (username == null || userpass == null) {
            return null;
        }
        User user = userDao.selectOne(username, userpass);
        return user;
    }

    /**
     * vip登录
     * @param username
     * @param password
     * @return
     */
    @Override
    public User vipLogin(String username, String password) {
        // 查询数据库中匹配的VIP用户
        User vipUser = userDao.selectVipByUsername(username);
        // 首先检查vipUser是否为null，避免空指针异常
        if (vipUser != null ) {
            // 用户名和加密密码匹配，验证成功
            if (BCryptUtils.verifyPassword(password, vipUser.getUserpass())) {
                // 用户名和加密密码匹配，验证成功
                return vipUser;
            }
        }
        // 没有找到匹配的用户或密码不正确，返回null
        return null;
    }

    /**
     * 普通用户登录
     * @param username
     * @param password
     * @return
     */
    @Override
    public User commonLogin(String username, String password) {
        // 查询数据库中匹配的普通用户
        User commonUser = userDao.selectCommonUserByUsername(username);
        // 首先检查commonUser是否为null，避免空指针异常
        if(commonUser !=null){
            if (BCryptUtils.verifyPassword(password, commonUser.getUserpass())) {
                // 用户名和加密密码匹配，验证成功
                return commonUser;
            }
        }
        // 没有找到匹配的用户或密码不正确，返回null
        return null;
    }


    /**
     * 用户添加
     * @param user
     * @return
     */
    @Override
    public Boolean add(User user) {
        List<User> users = userDao.selectAll();
        for (User user1 : users) {
            if(user1.getUsername().equals(user.getUsername())){
                return false;
            }
            if(user.getEmail() != null && user1.getEmail() != null && user1.getEmail().equals(user.getEmail())){
                return false;
            }
        }
//        添加bcrypt加密
        String password = BCryptUtils.encryptPassword(user.getUserpass());
        user.setUserpass(password);
        userDao.insert(user);
//        先根据插入的user查询到对应的id，然后根据id插入对应的权限：默认为（ROLE_common）
        Boolean result = userDao.insertAuthority(user.getUsername())>0;
        return result;
    }

    /**
     * 用户修改
     * @param loginParam
     * @return
     */
    @Override
    public Boolean update(LoginParam loginParam) {
        String key =RedisConstant.EMAIL+ loginParam.getEmail() ;
        System.out.println(key);
//        获取redis中存储的验证码
        String code = redisTemplate.opsForValue().get(key);
//        比较验证码是否和redis中存储的验证码一致
        if (code == null || !code.equals(loginParam.getCode())) {
            return false;
        }
        if (loginParam.getUserpass() != null) {
            loginParam.setUserpass(BCryptUtils.encryptPassword(loginParam.getUserpass()));
        }
        return userDao.update(loginParam) > 0;
    }

    @Override
    public Boolean delete(Integer id) {
        if (id != null ){
            return userDao.delete(id)>0;
        }
        return false;
    }

    @Override
    public List<User> selectUserByUsername(String username) {
        return userDao.selectUserByUsername(username);
    }

    @Override
    public List<User> findUserInfoByPage(Integer currentPage, Integer pageSize) {
        int offset = (currentPage - 1) * pageSize;
        return userDao.selectUserInfoByPage(offset, pageSize);
    }

    @Override
    public Long selectUserInfoCount() {
        return userDao.selectUserInfoCount();
    }

    @Override
//    @Transactional // 添加事务注解
    public Boolean updateUserInfo(User user) {
        userDao.updateUserInfo(user);
        return userDao.updateAuthority(user.getId(), user.getAuthority_id()) > 0;
    }
    //查询角色
    @Override
    public Integer selectAuthority(Integer userid) {
        return userDao.selectAuthority(userid);
    }


}
